<?php 
class ControllerProductTestimonialFeedback extends Controller {
	private $error = array(); 
    
	public function index() {
		$this->language->load('product/testimonial_feedback');

		$this->document->title = $this->language->get('heading_title');  
	
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {

			$this->document->title = $this->language->get('heading_title');
			
			$this->load->model('catalog/testimonial');
					
			if ($this->request->server['REQUEST_METHOD'] == 'POST') {
				$this->model_catalog_testimonial->addTestimonial($this->request->post);
				
				$this->session->data['success'] = $this->language->get('text_success');
				
				$this->redirect(HTTPS_SERVER . 'index.php?route=product/testimonial_feedback/success');
				
			}
			
		} 

		$this->document->breadcrumbs = array();

		$this->document->breadcrumbs[] = array(
			'href'      => HTTP_SERVER . 'index.php?route=common/home',
			'text'      => $this->language->get('text_home'),
			'separator' => FALSE
		);

		$this->document->breadcrumbs[] = array(
			'href'      => HTTP_SERVER . 'index.php?route=product/testimonial_feedback',
			'text'      => $this->language->get('heading_title'),
			'separator' => $this->language->get('text_separator')
		);
		
		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['entry_name'] = $this->language->get('entry_name');
		$this->data['entry_feedback'] = $this->language->get('entry_feedback');
		$this->data['entry_captcha'] = $this->language->get('entry_captcha');

		if (isset($this->error['name'])) {
			$this->data['error_name'] = $this->error['name'];
		} else {
			$this->data['error_name'] = '';
		}
		
		if (isset($this->error['feedback'])) {
			$this->data['error_feedback'] = $this->error['feedback'];
		} else {
			$this->data['error_feedback'] = '';
		}
		
		if (isset($this->error['captcha'])) {
			$this->data['error_captcha'] = $this->error['captcha'];
		} else {
			$this->data['error_captcha'] = '';
		}	

		$this->data['button_continue'] = $this->language->get('button_continue');
	
		$this->data['action'] = HTTP_SERVER . 'index.php?route=product/testimonial_feedback';
		$this->data['store'] = $this->config->get('config_store');
		
		if (isset($this->request->post['name'])) {
			$this->data['name'] = $this->request->post['name'];
		} else {
			$this->data['name'] = '';
		}

		if (isset($this->request->post['feedback'])) {
			$this->data['feedback'] = $this->request->post['feedback'];
		} else {
			$this->data['feedback'] = '';
		}
		
		if (isset($this->request->post['captcha'])) {
			$this->data['captcha'] = $this->request->post['captcha'];
		} else {
			$this->data['captcha'] = '';
		}		
	
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/testimonial_feedback.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/product/testimonial_feedback.tpl';
		} else {
			$this->template = 'default/template/product/testimonial_feedback.tpl';
		}
		
		$this->children = array(
			'common/header',
			'common/footer',
			'common/column_left',
			'common/column_right'
		);
		
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));		
	}
	

	public function success() {
		$this->language->load('product/testimonial_feedback');

		$this->document->title = $this->language->get('heading_title'); 

		$this->document->breadcrumbs = array();

		$this->document->breadcrumbs[] = array(
			'href'      => HTTP_SERVER . 'index.php?route=common/home',
			'text'      => $this->language->get('text_home'),
			'separator' => FALSE
		);

		$this->document->breadcrumbs[] = array(
			'href'      => HTTP_SERVER . 'index.php?route=product/testimonial_feedback',
			'text'      => $this->language->get('heading_title'),
			'separator' => $this->language->get('text_separator')
		);	
		
		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_message'] = $this->language->get('text_message');

		$this->data['button_continue'] = $this->language->get('button_continue');

		$this->data['continue'] = HTTP_SERVER . 'index.php?route=common/home';

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/success.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/common/success.tpl';
		} else {
			$this->template = 'default/template/common/success.tpl';
		}
		
		$this->children = array(
			'common/header',
			'common/footer',
			'common/column_left',
			'common/column_right'
		);
		
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression')); 
	}

	public function captcha() {
		$this->load->library('captcha');
		
		$captcha = new Captcha();
		
		$this->session->data['captcha'] = $captcha->getCode();
		
		$captcha->showImage();
	}
	
	private function validate() {
		if ((strlen(utf8_decode($this->request->post['name'])) < 3) || (strlen(utf8_decode($this->request->post['name'])) > 32)) {
			$this->error['name'] = $this->language->get('error_name');
		}

		if ((strlen(utf8_decode($this->request->post['feedback'])) < 10) || (strlen(utf8_decode($this->request->post['feedback'])) > 1000)) {
			$this->error['feedback'] = $this->language->get('error_feedback');
		}

		if (!isset($this->session->data['captcha']) || ($this->session->data['captcha'] != $this->request->post['captcha'])) {
			$this->error['captcha'] = $this->language->get('error_captcha');
		}
		
		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}  	  
	}
}
?>
